﻿namespace YidanSoft.Library.EditorUtility.WinControls
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using YidanSoft.Library.EditorUtility.General;

    [ToolboxBitmap(typeof(ColorComboBox), "YidanSoft.Library.EditorUtility.WinControls.ColorComboBox.bmp"), ToolboxItem(true)]
    public class ColorComboBox : ComboBoxBase
    {
        private const int PREVIEW_BOX_WIDTH = 20;

        public ColorComboBox()
        {
            base.DropDownStyle = ComboBoxStyle.DropDownList;
            base.Items.AddRange(ColorUtil.KnownColorNames);
        }

        public ColorComboBox(bool toolBarUse) : base(toolBarUse)
        {
            base.DropDownStyle = ComboBoxStyle.DropDownList;
            base.Items.AddRange(ColorUtil.KnownColorNames);
        }

        protected override void DrawComboBoxItem(Graphics g, Rectangle bounds, int Index, bool selected, bool editSel)
        {
            base.DrawComboBoxItem(g, bounds, Index, selected, editSel);
            if (Index != -1)
            {
                string name = base.Items[Index].ToString();
                Color color = Color.FromName(name);
                Brush brush = new SolidBrush(SystemColors.MenuText);
                Brush brush2 = new SolidBrush(color);
                g.FillRectangle(brush2, bounds.Left + 2, bounds.Top + 2, 20, bounds.Height - 4);
                brush2.Dispose();
                Pen pen = new Pen(Brushes.Black, 1f);
                g.DrawRectangle(pen, new Rectangle(bounds.Left + 1, bounds.Top + 1, 0x15, bounds.Height - 3));
                pen.Dispose();
                Size size = TextUtil.GetTextSize(g, base.Items[Index].ToString(), this.Font);
                int y = bounds.Top + ((bounds.Height - size.Height) / 2);
                g.DrawString(name, this.Font, brush, (PointF) new Point(bounds.Left + 0x1c, y));
                brush.Dispose();
            }
        }

        protected override void DrawComboBoxItemEx(Graphics g, Rectangle bounds, int Index, bool selected, bool editSel)
        {
            base.DrawComboBoxItemEx(g, bounds, Index, selected, editSel);
            if (Index != -1)
            {
                Color color = Color.FromName(base.Items[Index].ToString());
                SolidBrush brush = new SolidBrush(SystemColors.MenuText);
                Rectangle rectangle = bounds;
                rectangle.Inflate(-3, -3);
                Brush brush2 = new SolidBrush(color);
                g.FillRectangle(brush2, rectangle.Left + 2, rectangle.Top + 2, 20, rectangle.Height - 4);
                Pen pen = new Pen(Brushes.Black, 1f);
                g.DrawRectangle(pen, new Rectangle(rectangle.Left + 1, rectangle.Top + 1, 0x15, rectangle.Height - 3));
                pen.Dispose();
                Size size = TextUtil.GetTextSize(g, base.Items[Index].ToString(), this.Font);
                int y = bounds.Top + ((bounds.Height - size.Height) / 2);
                Rectangle layoutRectangle = new Rectangle(bounds.Left + 0x1f, y, ((bounds.Width - 0x1f) - 12) - 4, y + size.Height);
                g.DrawString(base.Items[Index].ToString(), this.Font, brush, layoutRectangle);
                brush.Dispose();
            }
        }

        protected override void DrawDisableState()
        {
            base.DrawDisableState();
            using (Graphics graphics = base.CreateGraphics())
            {
                using (Brush brush = new SolidBrush(SystemColors.ControlDark))
                {
                    Rectangle clientRectangle = base.ClientRectangle;
                    Rectangle rectangle2 = new Rectangle(clientRectangle.Left, clientRectangle.Top, clientRectangle.Width, clientRectangle.Height);
                    rectangle2.Inflate(-3, -3);
                    graphics.DrawRectangle(SystemPens.ControlDark, new Rectangle(rectangle2.Left + 2, rectangle2.Top + 2, 20, rectangle2.Height - 4));
                    int selectedIndex = this.SelectedIndex;
                    Size size = TextUtil.GetTextSize(graphics, base.Items[selectedIndex].ToString(), this.Font);
                    int y = clientRectangle.Top + ((clientRectangle.Height - size.Height) / 2);
                    Rectangle layoutRectangle = new Rectangle(clientRectangle.Left + 0x1f, y, ((clientRectangle.Width - 0x1f) - 12) - 4, y + size.Height);
                    graphics.DrawString(base.Items[selectedIndex].ToString(), this.Font, brush, layoutRectangle);
                }
            }
        }

        public void PassMsg(ref Message m)
        {
            base.WndProc(ref m);
        }
    }
}

